﻿using ChangchunPublicInterfacs;
using PublicModels.APIObjs;
using PublicModels.Eums;
using PublicModels.Results.PoiQueryResults;
using SpatialLite.Core.Geometries;
using static System.Formats.Asn1.AsnWriter;

namespace ChangchunPublicInfoWebSite.Common;

public class ResultPageHelper
{
    public ResultPageHelper(string keyword, TravelMode travelMode, SearchMode searchMode, IPoiQuery poiQuery, ITravelSubject travelSubject, Point center, int scope)
    {
        Keyword = keyword;
        TravelMode = travelMode;
        PoiQuery = poiQuery;
        SearchMode = searchMode;
        TravelSubject = travelSubject;
        MapCenter = center;
        Scope = scope;
    }

    public IPoiQuery PoiQuery { get; set; }

    public ITravelSubject TravelSubject { get; set; }

    public string Keyword { get; set; } = string.Empty;

    public TravelMode TravelMode { get; set; } = TravelMode.Driving;

    public SearchMode SearchMode { get; set; } = SearchMode.WholeCity;

    public int CurrentPage { get; set; } = -1;

    public Point MapCenter { get; set; } = new();

    public int Scope { get; set; } = 2000;

    public async Task<PoiQueryResult> NextPage()
    {
        CurrentPage += 1;
        var poiResult = new PoiQueryResult();
        switch (SearchMode)
        {
            case SearchMode.WholeCity:
                poiResult = await PoiQuery.KeywordQueryAsync(Keyword, CurrentPage);
                break;
            case SearchMode.Scoped:
                poiResult = await PoiQuery.CircleQueryAsync(Keyword, CurrentPage, MapCenter, Scope);
                break;
            case SearchMode.ScenicSpot:
                poiResult = await TravelSubject.ScenicSpotKeywordQueryAsync(Keyword, CurrentPage);
                break;
        }
        return poiResult;
    }

    public async Task<PoiQueryResult> PreviousPage()
    {
        if (CurrentPage is 0)
            return new PoiQueryResult()
            {
                StatusCode = Code.IllegalArgs,
                Message = "当前已在第一页",
            };
        CurrentPage -= 1;
        var poiResult = new PoiQueryResult();
        switch (SearchMode)
        {
            case SearchMode.WholeCity:
                poiResult = await PoiQuery.KeywordQueryAsync(Keyword, CurrentPage);
                break;
            case SearchMode.Scoped:
                poiResult = await PoiQuery.CircleQueryAsync(Keyword, CurrentPage, MapCenter, Scope);
                break;
            case SearchMode.ScenicSpot:
                poiResult = await TravelSubject.ScenicSpotKeywordQueryAsync(Keyword, CurrentPage);
                break;
        }
        return poiResult;
    }
}
